package com.enjoyor.soa.traffic.rest.efoms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.efoms.EvalGradeInfoDto;
import com.enjoyor.soa.traffic.api.dto.efoms.FileInfoDto;
import com.enjoyor.soa.traffic.api.dto.efoms.WorkordersRecordInfoDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RltRouteDeviceDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.rest.efoms.dubbo.IWorkordersRecordConsumeService;
import com.enjoyor.soa.traffic.rest.efoms.enums.EnumDicPublicRest;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/workordersRecord" })
@Api("维修工单操作rest接口")
@CrossOrigin
public class WorkordersRecordController {

	@Autowired
	private IWorkordersRecordConsumeService workordersRecordConsumeService;

	@PutMapping(value = { "/cancelWorkorders" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("撤销维修工单")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String"),
		@ApiImplicitParam(name = "operReasonCode", value = "撤销原因编号", paramType = "query", required = true, dataType = "String"),
		@ApiImplicitParam(name = "operReasonName", value = "撤销原因名称", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "operExplain", value = "撤销说明", paramType = "query", dataType = "String")})
    public ResultPojo cancelWorkorders(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		@RequestParam(defaultValue = "", value = "operReasonCode", required = true) String operReasonCode,
    		@RequestParam(defaultValue = "", value = "operReasonName", required = true) String operReasonName,
    		@RequestParam(defaultValue = "", value = "operExplain", required = false) String operExplain,
    		HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		String personName = userInfo.getPersonName();
		String operTypeCode = EnumDicPublicRest.ORD_OPER_DELETE.getKey();
		String operTypeName = EnumDicPublicRest.ORD_OPER_DELETE.getValue();
		WorkordersRecordInfoDto workordersRecordDto = new WorkordersRecordInfoDto(workordersId, operTypeCode,
				operTypeName, null, operReasonCode, operReasonName, null, null, operExplain);
        return workordersRecordConsumeService.insertWorkordersRecord(workordersRecordDto, null, null, personName);
    }
	
	@PutMapping(value = { "/pressWorkorders" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("催办维修工单")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "operExplain", value = "催办说明", paramType = "query", dataType = "String")})
    public ResultPojo pressWorkorders(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		@RequestParam(defaultValue = "", value = "operExplain", required = false) String operExplain,
    		HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		String personName = userInfo.getPersonName();
		String operTypeCode = EnumDicPublicRest.ORD_OPER_PRESS.getKey();
		String operTypeName = EnumDicPublicRest.ORD_OPER_PRESS.getValue();
		WorkordersRecordInfoDto workordersRecordDto = new WorkordersRecordInfoDto(workordersId, operTypeCode,
				operTypeName, null, null, null, null, null, operExplain);
        return workordersRecordConsumeService.insertWorkordersRecord(workordersRecordDto, null, null, personName);
    }
	
	@PutMapping(value = { "/postponeWorkorders" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("延期申请维修工单")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "deadlineDate", value = "申请完成时间", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "operExplain", value = "申请理由", paramType = "query", required = true, dataType = "String")})
    public ResultPojo postponeWorkorders(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		@RequestParam(defaultValue = "", value = "deadlineDate", required = true) String deadlineDate,
    		@RequestParam(defaultValue = "", value = "operExplain", required = true) String operExplain,
    		HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		String personName = userInfo.getPersonName();
		String operTypeCode = EnumDicPublicRest.ORD_OPER_POSTPONE.getKey();
		String operTypeName = EnumDicPublicRest.ORD_OPER_POSTPONE.getValue();
		String operResultCode = EnumDicPublicRest.OPER_NOHANDLE.getKey();
		String operResult = EnumDicPublicRest.OPER_NOHANDLE.getValue();
		WorkordersRecordInfoDto workordersRecordDto = new WorkordersRecordInfoDto(workordersId, operTypeCode,
				operTypeName, deadlineDate, null, null, operResultCode, operResult, operExplain);
        return workordersRecordConsumeService.insertWorkordersRecord(workordersRecordDto, null, null, personName);
    }
	
	@PutMapping(value = { "/checkWorkorders" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("延期审核维修工单")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "deadlineDate", value = "审核完成时间", paramType = "query", required = false, dataType = "String"),
    	@ApiImplicitParam(name = "isPass", value = "是否通过(1为通过，0为拒绝)", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "operExplain", value = "审核意见", paramType = "query", dataType = "String")})
    public ResultPojo checkWorkorders(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		@RequestParam(defaultValue = "", value = "deadlineDate", required = false) String deadlineDate,
    		@RequestParam(defaultValue = "", value = "isPass", required = true) String isPass,
    		@RequestParam(defaultValue = "", value = "operExplain", required = false) String operExplain,
    		HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		String personName = userInfo.getPersonName();
		String operTypeCode = EnumDicPublicRest.ORD_OPER_CHECK.getKey();
		String operTypeName = EnumDicPublicRest.ORD_OPER_CHECK.getValue();
		String operResultCode = null;
		String operResult = null;
		if("1".equals(isPass)) {
			operResultCode = EnumDicPublicRest.OPER_CHECKPASS.getKey();
			operResult = EnumDicPublicRest.OPER_CHECKPASS.getValue();
		}else if("0".equals(isPass)) {
			operResultCode = EnumDicPublicRest.OPER_CHECKREJECT.getKey();
			operResult = EnumDicPublicRest.OPER_CHECKREJECT.getValue();
		}else {
			return new ResultPojo(EnumAppCode.PARAM_ERR, "参数错误(isPass是否通过: 1为通过，0为拒绝)");
		}
		WorkordersRecordInfoDto workordersRecordDto = new WorkordersRecordInfoDto(workordersId, operTypeCode,
				operTypeName, deadlineDate, null, null, operResultCode, operResult, operExplain);
        return workordersRecordConsumeService.insertWorkordersRecord(workordersRecordDto, null, null, personName);
    }
	
	@PutMapping(value = { "/dispatchWorkorders" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("派发维修工单")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "opDeptId", value = "运维部门编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "opDeptName", value = "运维部门名称", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "opPersonId", value = "运维人员编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "opPersonName", value = "运维人员名称", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "deadlineDate", value = "维修期限时间", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "contactTel", value = "维修联系电话", paramType = "query", required = false, dataType = "String"),
    	@ApiImplicitParam(name = "operExplain", value = "派发说明", paramType = "query", required = false, dataType = "String")})
    public ResultPojo dispatchWorkorders(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		@RequestParam(defaultValue = "", value = "opDeptId", required = true) String opDeptId,
    		@RequestParam(defaultValue = "", value = "opDeptName", required = true) String opDeptName,
    		@RequestParam(defaultValue = "", value = "opPersonId", required = true) String opPersonId,
    		@RequestParam(defaultValue = "", value = "opPersonName", required = true) String opPersonName,
    		@RequestParam(defaultValue = "", value = "deadlineDate", required = true) String deadlineDate,
    		@RequestParam(defaultValue = "", value = "contactTel", required = false) String contactTel,
    		@RequestParam(defaultValue = "", value = "operExplain", required = false) String operExplain,
    		HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		String personName = userInfo.getPersonName();
		String operTypeCode = EnumDicPublicRest.ORD_OPER_DISPATCH.getKey();
		String operTypeName = EnumDicPublicRest.ORD_OPER_DISPATCH.getValue();
		WorkordersRecordInfoDto workordersRecordDto = new WorkordersRecordInfoDto(workordersId, operTypeCode,
				operTypeName, opDeptId, opDeptName, opPersonId, opPersonName, contactTel, deadlineDate, operExplain);
        return workordersRecordConsumeService.insertWorkordersRecord(workordersRecordDto, null, null, personName);
    }
	
	@PostMapping(value = { "/fackbackWorkorders" }, consumes = { "application/json; charset=UTF-8" }, produces = {
	"application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("反馈维修工单")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "failureTypeCode", value = "故障类型编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "failureTypeName", value = "故障类型名称", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "failureReason", value = "故障原因", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "isOver", value = "1为完成反馈，0为进度反馈", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "operExplain", value = "反馈说明", paramType = "query", required = false, dataType = "String"),
		@ApiImplicitParam(name = "fileInfoList", value = "关联附件信息(无附件输入[])", paramType = "body", required=false, dataType = "List<FileInfoDto>")})
    public ResultPojo fackbackWorkorders(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		@RequestParam(defaultValue = "", value = "failureTypeCode", required = true) String failureTypeCode,
    		@RequestParam(defaultValue = "", value = "failureTypeName", required = true) String failureTypeName,
    		@RequestParam(defaultValue = "", value = "failureReason", required = true) String failureReason,
    		@RequestParam(defaultValue = "", value = "isOver", required = true) String isOver,
    		@RequestParam(defaultValue = "", value = "operExplain", required = false) String operExplain,
    		@RequestBody List<FileInfoDto> fileInfoList,
    		HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		String personName = userInfo.getPersonName();
		String operTypeCode = EnumDicPublicRest.ORD_OPER_FACKBACK.getKey();
		String operTypeName = EnumDicPublicRest.ORD_OPER_FACKBACK.getValue();
		String operResultCode = null;
		String operResult = null;
		if("1".equals(isOver)) {
			operResultCode = EnumDicPublicRest.OPER_FACKBACK_OVER.getKey();
			operResult = EnumDicPublicRest.OPER_FACKBACK_OVER.getValue();
		}else if("0".equals(isOver)) {
			operResultCode = EnumDicPublicRest.OPER_FACKBACK_ING.getKey();
			operResult = EnumDicPublicRest.OPER_FACKBACK_ING.getValue();
		}else {
			return new ResultPojo(EnumAppCode.PARAM_ERR, "参数错误(isOver：1为完成反馈，0为进度反馈)");
		}
		WorkordersRecordInfoDto workordersRecordDto = new WorkordersRecordInfoDto(workordersId, operTypeCode,
				operTypeName, failureTypeCode, failureTypeName, failureReason, operResultCode, operResult, 
				operExplain, fileInfoList);
        return workordersRecordConsumeService.insertWorkordersRecord(workordersRecordDto, null, null, personName);
    }
	
	@PutMapping(value = { "/sureWorkorders" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("确认维修工单")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "isPass", value = "是否通过(1为通过，0为驳回)", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "isRecover", value = "是否恢复状态(1为是，0为否)", paramType = "query", required = false, dataType = "String"),
    	@ApiImplicitParam(name = "operExplain", value = "确认意见", paramType = "query", dataType = "String")})
    public ResultPojo sureWorkorders(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		@RequestParam(defaultValue = "", value = "isPass", required = true) String isPass,
    		@RequestParam(defaultValue = "", value = "isRecover", required = false) String isRecover,
    		@RequestParam(defaultValue = "", value = "operExplain", required = false) String operExplain,
    		HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		String personName = userInfo.getPersonName();
		String roleName = userInfo.getRoleName();
		String operTypeCode = EnumDicPublicRest.ORD_OPER_SURE.getKey();
		String operTypeName = EnumDicPublicRest.ORD_OPER_SURE.getValue();
		String operResultCode = null;
		String operResult = null;
		if("1".equals(isPass)) {
			operResultCode = EnumDicPublicRest.OPER_SUREPASS.getKey();
			operResult = EnumDicPublicRest.OPER_SUREPASS.getValue();
		}else if("0".equals(isPass)) {
			operResultCode = EnumDicPublicRest.OPER_SUREREJECT.getKey();
			operResult = EnumDicPublicRest.OPER_SUREREJECT.getValue();
		}else {
			return new ResultPojo(EnumAppCode.PARAM_ERR, "参数错误(isPass是否通过: 1为通过，0为驳回)");
		}
		WorkordersRecordInfoDto workordersRecordDto = new WorkordersRecordInfoDto(workordersId, operTypeCode,
				operTypeName, null, null, null, operResultCode, operResult, operExplain);
        return workordersRecordConsumeService.insertWorkordersRecord(workordersRecordDto, isRecover, roleName, personName);
    }
	
	@PostMapping(value = { "/evaluateWorkorders" }, consumes = { "application/json; charset=UTF-8" }, produces = {
	"application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("评价维修工单")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "operExplain", value = "评价意见", paramType = "query", required = false, dataType = "String"),
		@ApiImplicitParam(name = "evalGradeList", value = "评价项评分", paramType = "body", required=true, dataType = "List<EvalGradeInfoDto>")})
    public ResultPojo evaluateWorkorders(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		@RequestParam(defaultValue = "", value = "operExplain", required = false) String operExplain,
    		@RequestBody List<EvalGradeInfoDto> evalGradeList,
    		HttpServletRequest request) {
		UserDto userInfo = (UserDto) request.getAttribute("currentUser");
		String personName = userInfo.getPersonName();
		String operTypeCode = EnumDicPublicRest.ORD_OPER_EVALUATE.getKey();
		String operTypeName = EnumDicPublicRest.ORD_OPER_EVALUATE.getValue();
		WorkordersRecordInfoDto workordersRecordDto = new WorkordersRecordInfoDto(workordersId, operTypeCode,
				operTypeName, null, null, null, null, null, operExplain);
		//赋值评价项评分
		workordersRecordDto.setEvalGradeList(evalGradeList);
        return workordersRecordConsumeService.insertWorkordersRecord(workordersRecordDto, null, null, personName);
    }
	
	@PutMapping(value = { "/getNextProcess" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("获取下一步流程")
    @ApiImplicitParam(name = "workordersId", value = "维修工单编号", paramType = "query", required = true, dataType = "String")
    public ResultPojo getNextProcess(
    		@RequestParam(defaultValue = "", value = "workordersId", required = true) String workordersId,
    		HttpServletRequest request) {
		ResultPojo result = workordersRecordConsumeService.getNextProcess(workordersId);
        return result;
    }
}
